//
//  NithCell.m
//  房源、商铺、写字楼详情页
//
//  Created by MB__Lee on 2018/5/24.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "NithCell.h"

#import "MBNetTool.h"
#import "NCoCell.h"
#import "NiRecomandModel.h"
#import "MJExtension.h"

#import "MBHouseDetailVC.h"

@interface NithCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


//@property(nonatomic,strong)UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UILabel *RecommandLabel;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *mut;

@end

static NSString *NCoCell_Identi = @"NCoCell";
@implementation NithCell


-(NSMutableArray *)mut{
    if (!_mut) {
        _mut  =[NSMutableArray array];
    }
    return  _mut;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:NCoCell_Identi bundle:nil] forCellWithReuseIdentifier:NCoCell_Identi];
    
}
-(void)setModel:(MBFangDetailModel *)model{
    [super setModel:model];
    
    [self getSourceFromNet];
    
}

-(void)getSourceFromNet{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"category"] = self.model.category;
    dict[@"city"] = self.model.city_sn;
    dict[@"district"] = self.model.district_sn;
    dict[@"region"] = self.model.region_sn;
    
    //当前租金 ----测试使用
    NSUInteger rentValue = [self.model.rent integerValue];
//        NSUInteger Value_start = rentValue - 1000;
    NSUInteger Value_end = rentValue + 1000;
    
//        dict[@"rent_start"] = [NSString stringWithFormat:@"%zd",Value_start];
    dict[@"rent_end"] = [NSString stringWithFormat:@"%zd",Value_end];
    
    
    
    
    
    dict[@"bedroom"] = self.model.bedroom;
    dict[@"type"] = self.model.type;
    dict[@"decoration"] = self.model.decoration;
    dict[@"order"] =  @"recommend";
    
    
    
    
    [MBNetTool MBPost:Fang_list paramater:dict success:^(id response) {
        //        NSMutableArray *source = []
        
        if ([response[@"code"] integerValue] == 10000) {
            NSMutableArray *source = [NiRecomandModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            [self.mut addObjectsFromArray:source];
            
            NSLog(@"%zd",source.count);
            [_collectionView reloadData];
            
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
    [_collectionView reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return self.mut.count;
    
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NCoCell *cell  =[collectionView dequeueReusableCellWithReuseIdentifier:NCoCell_Identi forIndexPath:indexPath];
        cell.niModel = self.mut[indexPath.row];
    return  cell;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake(201, 213);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return  10;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return  15;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return  UIEdgeInsetsMake(0, 15, 15, 15);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NiRecomandModel *reModel = self.mut[indexPath.row];
    MBHouseDetailVC *vc = [[MBHouseDetailVC alloc] init];
    vc.reModel = reModel;
 
    if (self.niBlock) {
        self.niBlock(vc);
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
